package com.swxc.smartorder.ui.activity

import android.os.Bundle
import android.util.Log
import android.view.View
import com.lzy.okgo.model.HttpParams
import com.swxc.smartorder.R
import com.swxc.smartorder.app.GlobalAPI
import com.swxc.smartorder.entity.BackEntity
import com.swxc.smartorder.event.EventUtil
import com.swxc.smartorder.event.SimpleEvent
import com.swxc.smartorder.network.NetworkHelper
import com.swxc.smartorder.ui.base.BaseActivity
import com.swxc.smartorder.ui.dialog.PromptDialog
import com.swxc.smartorder.utils.JsonUtil
import kotlinx.android.synthetic.main.activity_validate_pwd.*


class ValidatePwdActivity : BaseActivity(), View.OnClickListener {
    var mobile: String? = null
    var firstPwd: String? = null
    private var second: String? = null
    private var networkHelper: NetworkHelper? = null

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> {
                finish()
            }
            R.id.btn_next -> {
                validatePwd()
            }
        }
    }

    /**
     * 验证输入的密码
     */
    private fun validatePwd() {
        firstPwd = edt_first.text.toString().trim()
        second = edt_second.text.toString().trim()
        if (firstPwd != second) {
            showToastCommon("两次输入的密码不一致")
        } else {
            val httpParams = HttpParams()
            httpParams.put("mobile", mobile)
            httpParams.put("password", second)

            networkHelper?.putRequestWithoutToken(GlobalAPI.modifyPwd, httpParams, object : NetworkHelper.NetWorkHelperBack {
                override fun requestSuccess(json: String) {
                    val promptDialog = PromptDialog()
                    val bundle = Bundle()
                    val fromJson = JsonUtil.fromJson<BackEntity>(json, BackEntity::class.java)
                            ?: return
                    when (fromJson.responseCode) {
                        1001 -> {
                            EventUtil.getInstance().postSticky(SimpleEvent(SimpleEvent.MODIFY_PWD, 0x0001))
                            readyGoThenKill(LoginActivity::class.java)

                        }
                        else -> {
                            bundle.putString("title", fromJson.message)
                            bundle.putBoolean("isSuccess", false)
                            bundle.putBoolean("dismiss", false)
                            promptDialog.arguments = bundle
                            promptDialog.show(supportFragmentManager, "promptDialog")
                        }
                    }
                    Log.e(BUG_TAG, json)
                }

                override fun requestFail() {
                    val promptDialog = PromptDialog()
                    val bundle = Bundle()
                    bundle.putString("title", "网络连接失败")
                    bundle.putBoolean("isSuccess", false)
                    promptDialog.arguments = bundle
                    promptDialog.show(supportFragmentManager, "promptDialog")
                }
            })

        }


    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_validate_pwd)
        initBundleData()
        initViewAndEvent()
        initRxJava()
    }

    private fun initBundleData() {
        mobile = intent.extras.getString("mobile")


    }

    private fun initRxJava() {


    }

    private fun initViewAndEvent() {

        networkHelper = NetworkHelper(this, supportFragmentManager)
        btn_next.isEnabled = true
        iv_back.setOnClickListener(this)
        btn_next.setOnClickListener(this)

    }

}
